﻿#region Copyright 2008 - Evolution Systems.
//
// Author: José Rogério Ferreira dos Santos(joserogerio.santos@gmail.com)
// Date: 24/04/2009
// Time: 21:17
// All rights are reserved. Reproduction or transmission in whole or in part, in
// any form or by any means, eletronic, mechanical or otherwise, is prohibited
// without the prior written permission of the author.
//
// Filename: ${FILE_NAME}.cs
#endregion
using System;
using System.Drawing;
using System.Windows.Forms;

using ComponentFactory.Krypton.Toolkit;
using csharp.common;

namespace csharp.rad.finan.entries
{
	/// <summary>
	/// Description of frmRegisterBpr.
	/// </summary>
	public partial class frmRegisterBpr : common.RegisterTemplate
	{
		public csharp.common.CONSTANTS.BILL_TYPE bill_type;
		
		private Bpr bpr = new Bpr();
		private BprDetails listBprDetails = new BprDetails();
		
		Configuration config = new Configuration();
		private general.Statuss listStatus 		 			= new general.Statuss();
		private CostCenters listCostCenter 		 			= new CostCenters();
		private general.Customers listCustomers 			= new general.Customers();
		private Bills 				  listBills     		= new Bills();
		//private SubClasss listSubClass 			 			= new SubClasss();
		//private Classs listClass					 		= new Classs();
		private SubGroups listSubgroups			 			= new SubGroups();
		private Groups    listGroups				 		= new Groups();
		private general.Types listCollectionType    		= new general.Types();
		private general.Types listDocumentType 		 		= new general.Types();
		private BillMvts listBillMvt 				 		= new BillMvts();
		private general.Suppliers  listSuppliers            = new general.Suppliers();
		
		private bool mutexGroup;
		private bool mutexSubGroup;
		//private bool mutexClass;
		private bool mutexSubClass;
		
		private bool portionsGenerated;
		
		public frmRegisterBpr()
		{
			//
			// The InitializeComponent() call is required for Windows Forms designer support.
			//
			InitializeComponent();
		}
		
		public frmRegisterBpr(string bprId)
		{
			InitializeComponent();
			
			bpr.id = bprId;
		}
		
		private void BtnGeneratePortionsClick(object sender, EventArgs e)
		{
			generatePortions();
		}
		
		private void generatePortions()
		{
			int numPortions, daysDiscount;
			DateTime aux;
			double portionValue, rest, total;
			
			numPortions 			= Convert.ToInt32(txtNumPortions.Text);
			total 					= common.Util.formatDouble(txtTotalValue.Text);
			aux 					= dtpFirstExpiration.Value;
			daysDiscount 			=  Convert.ToDateTime(dtpDiscountUntil.Text).Subtract(aux).Days;
			bpr.firstExpirationDate = aux.ToString();
			
			portionValue 			= Math.Round(total / numPortions, 2);
			rest 					= Math.Round(total - (portionValue*numPortions), 2);
			
			listBprDetails.clear();
			for(int i=0; i < numPortions; i++)
			{
				//Soma o resto na ultima parcela
				if (i+1 == numPortions)
					portionValue += rest;
				
				BprDetail tmp = new BprDetail();
				
				tmp.number = (i+1).ToString();
				tmp.historic = txtDescription.Text;
				/*if (i > 9)
					tmp.documentNumber = "000"+(i+1).ToString();
				else if (i > 99)
					tmp.documentNumber = "00"+(i+1).ToString();
				else
					tmp.documentNumber = "0"+(i+1).ToString();*/
				tmp.documentNumber = txtDocumentNumber.Text;
				
				tmp.expirationDate 	= aux.ToString("dd/MM/yyyy");
				tmp.value 			= portionValue.ToString("0.00");
				tmp.jurosValue 		= "0.00";
				tmp.penaltyValue 	= "0.00";
				tmp.paidValue 		= "0.00";
				tmp.penaltyAfterDays = txtPenaltyAfterDays.Text;
				tmp.percJuros		 = txtJuros.Text;
				tmp.discountValue    = txtDiscount.Text;
				tmp.percPenalty      = txtPenalty.Text;

				if(cmbDocumentType.SelectedIndex >= 0)
					tmp.documentTypeId = (cmbDocumentType.SelectedItem as general.TType).id;
				else
					tmp.documentTypeId = listDocumentType.get(0).id;
				
				if (cmbStatus.SelectedIndex >= 0)
					tmp.statusId = (cmbStatus.SelectedItem as general.Status).id;
				else
					tmp.statusId = listStatus.get(0).id;
				
				if(cmbCollectionType.SelectedIndex >= 0)
					tmp.collectionTypeId = (cmbCollectionType.SelectedItem as general.TType).id;
				else
					tmp.collectionTypeId = listCollectionType.get(0).id;
				
				tmp.discountUntil = aux.AddDays(daysDiscount).ToString("dd/MM/yyyy");
				tmp.userId = Information.userId;
				
				listBprDetails.add(tmp);
				aux = aux.AddMonths(1);
			}
			fillDataGridPortions();
		}
		
		private void fillDataGridPortions()
		{
			dgvPortions.Rows.Clear();
			portionsGenerated = false;
			string status_value;
			
			for(int i=0; i < listBprDetails.count(); i++)
			{
				dgvPortions.Rows.Add(new String[] {listBprDetails.get(i).id,
				                     	listBprDetails.get(i).number,
				                     	listBprDetails.get(i).documentNumber,
				                     	Convert.ToDateTime(listBprDetails.get(i).expirationDate).ToString(common.CONSTANTS.dateFormatBrazilian),
				                     	listBprDetails.get(i).value,
				                     	listBprDetails.get(i).jurosValue,
				                     	listBprDetails.get(i).penaltyValue,
				                     	listBprDetails.get(i).paidValue});
				if(listStatus.search(listBprDetails.get(i).statusId))
				{
					status_value = listStatus.get().value;
					if(status_value == "002")
						dgvPortions.Rows[i].DefaultCellStyle.BackColor = Color.Red;
					else if(status_value == "003")
						dgvPortions.Rows[i].DefaultCellStyle.BackColor = Color.Gray;
					else if(status_value == "004")
						dgvPortions.Rows[i].DefaultCellStyle.BackColor = Color.Yellow;
				}
				portionsGenerated = true;
			}
		}
		
		private void setDefaultOptions()
		{
			if (bill_type == CONSTANTS.BILL_TYPE.TOPAY)
			{
				Functions.setSelectIndexComboGroup(cmbGroup, config.toPayGroupId);
				Functions.setSelectIndexComboSubGroup(cmbSubGroup, config.toPaySubGroupId);
				//Functions.setSelectIndexComboClass(cmbClass, config.toPayClassId);
				//Functions.setSelectIndexComboSubClass(cmbSubClass, config.toPaySubClassId);
				Functions.setSelectIndexComboBill(cmbBill, config.toPayBillId);
			}
			else
			{
				Functions.setSelectIndexComboGroup(cmbGroup, config.toReceiveGroupId);
				Functions.setSelectIndexComboSubGroup(cmbSubGroup, config.toReceiveSubGroupId);
				//Functions.setSelectIndexComboClass(cmbClass, config.toReceiveClassId);
				//Functions.setSelectIndexComboSubClass(cmbSubClass, config.toReceiveSubClassId);
				Functions.setSelectIndexComboBill(cmbBill, config.toReceiveBillId);
			}
			
			txtNumPortions.Text 		= "1";
			txtTotalValue.Text  		= "0,00";
			txtDiscount.Text 			= "0,00";
			txtJuros.Text   			= "0";
			txtPenalty.Text 			= "0";
			txtPenaltyAfterDays.Text 	= "5";
			
			if (cmbStatus.Items.Count > 0)
				cmbStatus.SelectedIndex 	= 0;
			
			if (cmbCostCenter.Items.Count == 1)
				cmbCostCenter.SelectedIndex = 0;
		}
		
		private void fillComboStatus()
		{
			int tam = listStatus.count();
			for(int i=0; i < tam; i++)
				cmbStatus.Items.Add(listStatus.get(i));
			cmbStatus.DisplayMember = "name";
			cmbStatus.ValueMember 	= "id";
		}
		
		private void fillComboCostCenter()
		{
			int tam = listCostCenter.count();
			for(int i=0; i < tam; i++)
				cmbCostCenter.Items.Add(listCostCenter.get(i));
			cmbCostCenter.DisplayMember = "name";
			cmbCostCenter.ValueMember 	= "id";
		}
		
		private void fillComboCustomers()
		{
			int tam = listCustomers.count();
			for(int i=0; i < tam; i++)
				cmbCustomerName.Items.Add(listCustomers.get(i).person);
			cmbCustomerName.DisplayMember 	= "name";
			cmbCustomerName.ValueMember 	= "id";
		}
		
		private void fillComboCollectionType()
		{
			int tam = listCollectionType.count();
			
			for(int i=0; i < tam; i++)
				cmbCollectionType.Items.Add(listCollectionType.get(i));
			cmbCollectionType.DisplayMember = "name";
			cmbCollectionType.ValueMember 	= "id";
		}
		
		private void fillComboDocumentType()
		{
			int tam = listDocumentType.count();
			
			for(int i=0; i < tam; i++)
				cmbDocumentType.Items.Add(listDocumentType.get(i));
			cmbDocumentType.DisplayMember 	= "name";
			cmbDocumentType.ValueMember 	= "id";
		}
		
		private void fillComboBillMvt()
		{
			for (int i=0; i < listBillMvt.count(); i++)
				cmbBillMvt.Items.Add(listBillMvt.get(i));
			cmbBillMvt.DisplayMember = "name";
			cmbBillMvt.ValueMember   = "id";
		}
		
		private void setSelectedIndexComboStatus(string id)
		{
			for (int i=0; i < listStatus.count(); i++)
			{
				if (listStatus.get(i).id.Equals(id))
				{
					cmbStatus.SelectedIndex = i;
					break;
				}
			}
		}
		
		private void setSelectedIndexComboCostCenter(string id)
		{
			for (int i=0; i < listCostCenter.count(); i++)
			{
				if (listCostCenter.get(i).id.Equals(id))
				{
					cmbCostCenter.SelectedIndex = i;
					break;
				}
			}
		}
		
		private void setSelectedIndexComboCustomers(string person_id)
		{
			if (bill_type == CONSTANTS.BILL_TYPE.TOPAY)
			{
				for (int i=0; i < listSuppliers.count(); i++)
				{
					if (listSuppliers.get(i).person.id.Equals(person_id))
					{
						cmbCustomerName.SelectedIndex = i;
						break;
					}
				}				
			}
			else
			{
				for (int i=0; i < listCustomers.count(); i++)
				{
					if (listCustomers.get(i).person.id.Equals(person_id))
					{
						cmbCustomerName.SelectedIndex = i;
						break;
					}
				}
			}
		}
		
		private void setSelectedIndexComboCollectionType(string id)
		{
			for (int i=0; i < listCollectionType.count(); i++)
			{
				if (listCollectionType.get(i).id.Equals(id))
				{
					cmbCollectionType.SelectedIndex = i;
					break;
				}
			}
		}
		
		private void setSelectedIndexComboDocumentType(string id)
		{
			for (int i=0; i < listDocumentType.count(); i++)
			{
				if (listDocumentType.get(i).id.Equals(id))
				{
					cmbDocumentType.SelectedIndex = i;
					break;
				}
			}
		}
		
		private void setSelectedIndexComboBillMvt(string id)
		{
			for (int i=0; i < listBillMvt.count(); i++)
			{
				if (listBillMvt.get(i).id.Equals(id))
				{
					cmbBillMvt.SelectedIndex = i;
					break;
				}
			}
		}
		
		private void TxtCustomerCodeLeave(object sender, EventArgs e)
		{
			if (bill_type == CONSTANTS.BILL_TYPE.TOPAY)
			{
				searchSupplierByCode(txtCustomerCode.Text);
			}
			else
			{
				searchCustomerByCode(txtCustomerCode.Text);
			}
		}
		
		private void searchCustomerByCode(string code)
		{
			if (listCustomers.searchByCode(code))
				cmbCustomerName.SelectedIndex = listCustomers.getIndex();
		}
		
		private void searchSupplierByCode(string code)
		{
			if (listSuppliers.searchByCode(code))
				cmbCustomerName.SelectedIndex = listSuppliers.getIndex();
		}

		private void CmbGroupSelectedValueChanged(object sender, EventArgs e)
		{
			if (cmbGroup.SelectedIndex >= 0)
			{
				if (!mutexGroup)
				{
					listSubgroups.filterByGroup((cmbGroup.SelectedItem as Group).id);
					
					Functions.fillComboSubGroups(listSubgroups, cmbSubGroup, "id", "name");
					
					cmbSubGroup.Text 	= string.Empty;
					//cmbClass.Text 		= string.Empty;
					//cmbSubClass.Text 	= string.Empty;
					
					listSubgroups.disableFilter();
				}
			}
		}
		
		private void CmbSubGroupSelectedValueChanged(object sender, EventArgs e)
		{
			if (cmbSubGroup.SelectedIndex >= 0)
			{
				//fill the combos one level below
				if (!mutexSubGroup)
				{
					//listClass.filterBySubGroup((cmbSubGroup.SelectedItem as SubGroup).id);
					
					//Functions.fillComboClass(listClass, cmbClass, "id", "name");
					
					//cmbClass.SelectedIndex = -1;
					
					//listClass.disableFilter();
					
					listBills.filterSubGroup((cmbSubGroup.SelectedItem as SubGroup).id);
					
					finan.Functions.fillComboBill(listBills, cmbBill, "id", "name");
					
					cmbBill.SelectedIndex = -1;
					
					listBills.disableFilter();					
				}
				
				//set the combo one level above
				mutexGroup = true;
				Functions.setSelectIndexComboGroup(cmbGroup, (cmbSubGroup.SelectedItem as SubGroup).groupId);
				mutexGroup = false;
			}
		}
	
		#region Old code 2
		/*
		private void CmbClassSelectedValueChanged(object sender, EventArgs e)
		{
			if (cmbClass.SelectedIndex >= 0)
			{
				if (!mutexClass)
				{
					listSubClass.filterByClass((cmbClass.SelectedItem as Class).id);
					
					Functions.fillComboSubClass(listSubClass, cmbSubClass, "id", "name");
					
					cmbSubClass.SelectedIndex = -1;
					
					listSubClass.disableFilter();
				}
				
				mutexSubGroup = true;
				Functions.setSelectIndexComboSubGroup(cmbSubGroup, (cmbClass.SelectedItem as Class).subGroupId);
				mutexSubGroup = false;
			}
		}
		
		private void CmbSubClassSelectedValueChanged(object sender, EventArgs e)
		{
			if (cmbSubClass.SelectedIndex >= 0)
			{
				if (!mutexSubClass)
				{
					listBills.filterSubClass((cmbSubClass.SelectedItem as SubClass).id);
					//fillDataGridView();
					
					finan.Functions.fillComboBill(listBills, cmbBill, "id", "name");
					
					cmbBill.SelectedIndex = -1;
					
					listBills.disableFilter();
				}
				
				mutexClass = true;
				Functions.setSelectIndexComboClass(cmbClass, (cmbSubClass.SelectedItem as SubClass).classId);
				mutexClass = false;
			}
		}
		*/
		#endregion
		
		private void CmbBillSelectedValueChanged(object sender, EventArgs e)
		{
			if (cmbBill.SelectedIndex >= 0)
			{
				mutexSubClass = true;
				Functions.setSelectIndexComboSubGroup(cmbSubGroup, (cmbBill.SelectedItem as Bill).subgroupId);
				mutexSubClass = false;
				
				//listBills.fillList((cmbSubClass.SelectedItem as SubClass).id);
				//fillDataGridView();
				
				//finan.Functions.fillComboBill(listBills, cmbBill, "id", "name");
			}
		}
		
		private void TabPortionsEnter(object sender, EventArgs e)
		{
			txtNumPortions.Focus();
		}
		
		
		#region overrided methods
		protected override void newEvent()
		{
			setDefaultOptions();
			tabPrincipal.Focus();
			txtDocumentNumber.Focus();
		}
		
		/// <summary>
		/// Set data from edits to class
		/// </summary>
		protected override void setClass()
		{
			bpr.documentNumber 	= txtDocumentNumber.Text;
			bpr.historic 		= txtDescription.Text;
			bpr.statusId 		= (cmbStatus.SelectedItem as general.Status).id;
			
			if (bill_type == CONSTANTS.BILL_TYPE.TOPAY)
			{
				bpr.type 		= "D"; //Debit
				bpr.personId 	= (cmbCustomerName.SelectedItem as general.Person).id;
			}
			else
			{
				//listCustomers.searchByPersonId(bpr.personId);
				//txtCustomerCode.Text = listCustomers.get().person.code;
				bpr.personId 	= (cmbCustomerName.SelectedItem as general.Person).id;
				bpr.type = "C"; //Credit
			}
			
			bpr.collectionTypeId 	= (cmbCollectionType.SelectedItem as general.TType).id;
			bpr.documentTypeId   	= (cmbDocumentType.SelectedItem as general.TType).id;
			bpr.billMvtId			= (cmbBillMvt.SelectedItem as finan.BillMvt).id;
			bpr.costCenterId		= (cmbCostCenter.SelectedItem as finan.CostCenter).id;
			
			bpr.percJuros 			= txtJuros.Text;
			bpr.percPenalty			= txtPenalty.Text;
			bpr.penaltyAfterDays 	= txtPenaltyAfterDays.Text;
			bpr.emissionDate		= dtpEmissionDate.Text;
			
			bpr.billId 				= (cmbBill.SelectedItem as finan.Bill).id;
			bpr.obs 				= txtObs.Text;
			
			bpr.value				= txtTotalValue.Text;
			bpr.discount			= txtDiscount.Text;
			bpr.discountUntil		= dtpDiscountUntil.Text;
			
			bpr.companyId = Information.companyId;
			bpr.userId = Information.userId;
		}
		
		/// <summary>
		/// Set data from class to edits
		/// </summary>
		protected override void setEdits()
		{
			txtDocumentNumber.Text 		= bpr.documentNumber;
			txtDescription.Text 		= bpr.historic;
			setSelectedIndexComboStatus(bpr.statusId);
			
			setSelectedIndexComboCustomers(bpr.personId);
			setSelectedIndexComboCollectionType(bpr.collectionTypeId);
			setSelectedIndexComboBillMvt(bpr.billMvtId);
			setSelectedIndexComboDocumentType(bpr.documentTypeId);
			setSelectedIndexComboCostCenter(bpr.costCenterId);
			
			txtJuros.Text 				= bpr.percJuros;
			txtPenalty.Text 			= bpr.percPenalty;
			txtPenaltyAfterDays.Text	= bpr.penaltyAfterDays;
			dtpEmissionDate.Text 		= bpr.emissionDate;
			
			Functions.setSelectIndexComboBill(cmbBill, bpr.billId);
			txtObs.Text = bpr.obs;
			
			
			listBprDetails.fillListByBprId(bpr.id);
			txtNumPortions.Text 	= listBprDetails.count().ToString();
			txtTotalValue.Text 		= bpr.value;
			txtDiscount.Text 		= bpr.discount;
			dtpFirstExpiration.Text = listBprDetails.get(0).expirationDate;
			dtpDiscountUntil.Text 	= bpr.discountUntil;
			
			fillDataGridPortions();
		}
		
		/// <summary>
		/// Fill the DataGridView
		/// </summary>
		protected override void fillDataGridView()
		{
			
		}
		
		/// <summary>
		/// Find the object of search
		/// </summary>
		protected override void find()
		{
			common.Util.searchInGrid(dgvMain,"name", txtSearch.Text);
		}
		
		/// <summary>
		/// Save the register in the database.
		/// </summary>
		/// <returns>bool - true if the register was saved sucessful, false case not.</returns>
		protected override bool save()
		{
			if(!bpr.id.Equals(string.Empty))
			{
				return update();
			}
			if (!portionsGenerated)
			{
				KryptonMessageBox.Show("Parcelas não geradas, registro não pode ser salvo!");
				return false;
			}
			
			if (bill_type == CONSTANTS.BILL_TYPE.TOPAY)
			{
				config.toPayGroupId 		= (cmbGroup.SelectedItem as Group).id;
				config.toPaySubGroupId 		= (cmbSubGroup.SelectedItem as SubGroup).id;
				//config.toPayClassId  		= (cmbClass.SelectedItem as Class).id;
				//config.toPaySubClassId  	= (cmbSubClass.SelectedItem as SubClass).id;
				config.toPayBillId 			= (cmbBill.SelectedItem as Bill).id;
			}
			else
			{
				config.toReceiveGroupId 	= (cmbGroup.SelectedItem as Group).id;
				config.toReceiveSubGroupId 	= (cmbSubGroup.SelectedItem as SubGroup).id;
				//config.toReceiveClassId  	= (cmbClass.SelectedItem as Class).id;
				//config.toReceiveSubClassId 	= (cmbSubClass.SelectedItem as SubClass).id;
				config.toReceiveBillId 		= (cmbBill.SelectedItem as Bill).id;
			}
			config.doSerialization();

			common.Database.BeginTransaction(System.Data.IsolationLevel.ReadCommitted);
			
			if (!bpr.save())
			{
				common.Database.RollBack();
				return false;
			}
			
			for(int i=0; i < listBprDetails.count(); i++)
			{
				listBprDetails.get(i).bprId= bpr.id;
				if (!listBprDetails.get(i).save())
				{
					//KryptonMessageBox.Show();
					//set the message error
					bpr.errorMessage = "Erro ao tentar gravar parcela "
						+listBprDetails.get(i).number+", "
						+listBprDetails.get(i).errorMessage;
					common.Database.RollBack();
					return false;
				}
			}
			
			common.Database.Commit();
			return true;
		}
		
		/// <summary>
		/// Update the register in the database
		/// </summary>
		/// <returns>bool - true if the register was updated sucessful, false case not.</returns>
		protected override bool update()
		{
			if (!portionsGenerated)
			{
				KryptonMessageBox.Show("Parcelas não geradas, registro não pode ser salvo!");
				return false;
			}
			
			if (bill_type == CONSTANTS.BILL_TYPE.TOPAY)
			{
				config.toPayGroupId 		= (cmbGroup.SelectedItem as Group).id;
				config.toPaySubGroupId 	 	= (cmbSubGroup.SelectedItem as SubGroup).id;
				//config.toPayClassId  		= (cmbClass.SelectedItem as Class).id;
				//config.toPaySubClassId  	= (cmbSubClass.SelectedItem as SubClass).id;
				config.toPayBillId 			= (cmbBill.SelectedItem as Bill).id;
			}
			else
			{
				config.toReceiveGroupId 	= (cmbGroup.SelectedItem as Group).id;
				config.toReceiveSubGroupId 	= (cmbSubGroup.SelectedItem as SubGroup).id;
				//config.toReceiveClassId  	= (cmbClass.SelectedItem as Class).id;
				//config.toReceiveSubClassId 	= (cmbSubClass.SelectedItem as SubClass).id;
				config.toReceiveBillId 		= (cmbBill.SelectedItem as Bill).id;
			}
			config.doSerialization();
			common.Database.BeginTransaction(System.Data.IsolationLevel.ReadCommitted);
			
			if (!bpr.update())
			{
				common.Database.RollBack();
				return false;
			}
			
			for(int i=0; i < listBprDetails.count(); i++)
			{
				if (!listBprDetails.get(i).update())
				{
					//KryptonMessageBox.Show();
					//set the message error
					bpr.errorMessage = "Erro ao tentar gravar parcela "
						+listBprDetails.get(i).number+", "
						+listBprDetails.get(i).errorMessage;
					common.Database.RollBack();
					return false;
				}
			}
			
			common.Database.Commit();
			return true;
		}
		
		/// <summary>
		/// Delete the register in the database.
		/// </summary>
		/// <returns>bool - true if the register was deleted sucessful, false case not.</returns>
		protected override bool delete()
		{
			return false;
		}
		
		/// <summary>
		/// Method call in initialization
		/// </summary>
		protected override void init()
		{
			config.deserialize();
			listStatus.fillList("bpr");
			listCostCenter.fillList();
			listBills.fillList();
			listBillMvt.fillList();
			
			listCollectionType.fillList("collection");
			listDocumentType.fillList("document");
			
			//Fill groups
			listGroups.fillList();
			finan.Functions.fillComboGroups(listGroups, cmbGroup, "id", "name");
			
			//Fill subgroups
			listSubgroups.fillList();
			finan.Functions.fillComboSubGroups(listSubgroups, cmbSubGroup, "id", "name");
			
			//Fill classes
			//listClass.fillList();
			//finan.Functions.fillComboClass(listClass, cmbClass, "id", "name");
			
			//Fill subclasses
			//listSubClass.fillList();
			//finan.Functions.fillComboSubClass(listSubClass, cmbSubClass, "id", "name");
			
			listBills.fillList();
			finan.Functions.fillComboBill(listBills, cmbBill, "id", "name");
			
			fillComboStatus();
			fillComboCostCenter();
			fillComboBillMvt();
			
			fillComboCollectionType();
			fillComboDocumentType();
			
			//setDefaultOptions();
			
			//Hide the search tab
			tabSearchVisible = false;

			if (bill_type == CONSTANTS.BILL_TYPE.TOPAY)
			{
				listSuppliers.fillList();
				fillComboSuppliers();
			}
			else
			{
				listCustomers.fillListActive();
				fillComboCustomers();
			}
		}
		#endregion
		void fillComboSuppliers()
		{
			int tam = listSuppliers.count();
			
			for(int i=0; i < tam; i++)
			{
				cmbCustomerName.Items.Add(listSuppliers.get(i).person);
			}
			
			cmbCustomerName.ValueMember 	= "id";
			cmbCustomerName.DisplayMember 	= "name";
		}
		
		void CmbCustomerNameSelectedValueChanged(object sender, EventArgs e)
		{
			string code;
			
			if (bill_type == CONSTANTS.BILL_TYPE.TOPAY)
			{
				if(cmbCustomerName.SelectedIndex >= 0)
				{
					code = listSuppliers.get(cmbCustomerName.SelectedIndex).person.code;
					if (!code.Equals(txtCustomerCode.Text))
					{
						txtCustomerCode.Text = code;
						cmbCollectionType.Focus();
					}
				}
			}
			else
			{
				if (cmbCustomerName.SelectedIndex >= 0)
				{
					code = listCustomers.get(cmbCustomerName.SelectedIndex).person.code;
					if (!code.Equals(txtCustomerCode.Text))
					{
						txtCustomerCode.Text = code;
						cmbCollectionType.Focus();
					}
				}
			}

		}
		
		void FrmRegisterBprLoad(object sender, EventArgs e)
		{
			//base.Load();
			
			if (bill_type == CONSTANTS.BILL_TYPE.TOPAY)
			{
				this.Text = "Contas a Pagar";
				lblCustomer.Text = "Fornecedor";
			}
			else
			{
				this.Text = "Contas a Receber";
				lblCustomer.Text = "Cliente";
			}			
			
			
			BtnNewClick(sender, e);
			if (!bpr.id.Equals(string.Empty))
			{
				if (bpr.get())
				{
					setEdits();
				}
				else
				{
					KryptonMessageBox.Show("Erro ao tentar recuperar Conta:\n"+bpr.errorMessage);
					this.Close();
				}
			}
		}
		
		void BtnCancelClick(object sender, EventArgs e)
		{
			tabPrincipal.Focus();
		}
		
		void KryptonButton1Click(object sender, EventArgs e)
		{
			if(dgvPortions.SelectedRows.Count > 0)
			{
				frmBprDetail form = new frmBprDetail();
				String id = dgvPortions.CurrentRow.Cells["id"].Value.ToString();
				form.bprDetail.id = id;
				form.ShowDialog();
				listBprDetails.fillListByBprId(bpr.id);
				fillDataGridPortions();
			}
			else
			{
				KryptonMessageBox.Show("Selecione um item!");
			}
		}
	}
}
